package com.example.a828502.sfdialtestdemo;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 功能简化，备用代码，，，，，，
 * Created by 828502 on 2016/5/17.
 */


public class MainActivity extends FragmentActivity implements View.OnClickListener {
    //定义3个Fragment的对象
    private PSTNCallbackFragment fgPSTNCallback;
    private PSTNFragment fg2PSTN;
    //帧布局对象,就是用来存放Fragment的容器
    private FrameLayout flayout;
    //定义底部导航栏的三个布局
    private RelativeLayout course_layout;
    private RelativeLayout found_layout;
    private TextView mTxtCallback;
    private TextView mTxtCall;
    //定义要用的颜色值
    private int white = 0xFFFFFFFF;
    private int gray = 0xFF7597B3;
    private int blue =0xFF0AB2FB;
    //定义FragmentManager对象
    FragmentManager fManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fManager = getSupportFragmentManager();
        initViews();
    }
    public void initViews(){
        mTxtCallback = (TextView) findViewById(R.id.textCallback);
        mTxtCall = (TextView) findViewById(R.id.textCall);
//        course_layout = (RelativeLayout) findViewById(R.id.course_layout);
//        found_layout = (RelativeLayout) findViewById(R.id.found_layout);
        mTxtCallback.setOnClickListener(this);
        mTxtCall.setOnClickListener(this);
        setChioceItem(0);//默认显示第一个页面
    }
    //重写onClick事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textCallback:
                setChioceItem(0);
                break;
            case R.id.textCall:
                setChioceItem(1);
                break;
            default:
                break;
        }

    }

    //定义一个选中一个item后的处理
    public void setChioceItem(int index)
    {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fManager.beginTransaction();
        clearChioce();
        hideFragments(transaction);
        switch (index) {
            case 0:
                mTxtCallback.setTextColor(blue);
                mTxtCallback.setBackgroundColor(gray);
//                course_layout.setBackgroundResource(R.drawable.ic_launcher);
                if (fgPSTNCallback == null) {
                    // 如果fgPSTNCallback为空，则创建一个并添加到界面上
                    fgPSTNCallback = new PSTNCallbackFragment();
                    transaction.add(R.id.content, fgPSTNCallback);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fgPSTNCallback);
                }
                break;

            case 1:
                mTxtCall.setTextColor(blue);
                mTxtCall.setBackgroundColor(gray);
//                found_layout.setBackgroundResource(R.drawable.ic_launcher);
                if (fg2PSTN == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    fg2PSTN = new PSTNFragment();
                    transaction.add(R.id.content, fg2PSTN);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fg2PSTN);
                }
                break;
        }
        transaction.commit();
    }
    //隐藏所有的Fragment,避免fragment混乱
    private void hideFragments(FragmentTransaction transaction) {
        if (fgPSTNCallback != null) {
            transaction.hide(fgPSTNCallback);
        }
        if (fg2PSTN != null) {
            transaction.hide(fg2PSTN);
        }
    }

    //定义一个重置所有选项的方法
    public void clearChioce()
    {
        mTxtCallback.setBackgroundColor(white);
        mTxtCallback.setTextColor(gray);
        mTxtCall.setBackgroundColor(white);
        mTxtCall.setTextColor(gray);
    }

}

